package cn.seecoder.courselearning.serviceimpl.post;


import cn.seecoder.courselearning.mapperservice.post.PostMapper;
import cn.seecoder.courselearning.po.course.Course;
import cn.seecoder.courselearning.po.order.CourseOrder;
import cn.seecoder.courselearning.po.post.Post;
import cn.seecoder.courselearning.service.course.CourseService;
import cn.seecoder.courselearning.service.order.QueryOrderService;
import cn.seecoder.courselearning.service.post.PostService;
import cn.seecoder.courselearning.service.user.UserService;
import cn.seecoder.courselearning.util.Constant;
import cn.seecoder.courselearning.util.PageInfoUtil;
import cn.seecoder.courselearning.vo.course.CourseVO;
import cn.seecoder.courselearning.vo.post.PostVO;
import cn.seecoder.courselearning.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class PostServiceImpl implements PostService{
    @Resource
    private PostMapper postMapper;

    private QueryOrderService orderService;

    private CourseService courseService;

    private UserService userService;

    @Autowired
    public void setOrderService(QueryOrderService orderService) {
        this.orderService = orderService;
    }

    @Autowired
    public void setCourseService(CourseService courseService){this.courseService = courseService;}

    @Autowired
    public void setUserService(UserService userService){this.userService = userService;}
    @Override
    public ResultVO<PostVO> createPost(PostVO postVO) {
        postVO.setCreateTime(new Date());

        if(postVO.getUserId()!=null && postVO.getUserId()>0){
            Post post = new Post(postVO);
            String userName = userService.getUser(postVO.getUserId()).getUname();
            String courseName = courseService.getCourse(postVO.getCourseId(),postVO.getUserId()).getName();
            boolean bought=courseService.getCourse(postVO.getCourseId(),postVO.getUserId()).isBought();
            boolean manageable = courseService.getCourse(postVO.getCourseId(),postVO.getUserId()).isManageable();
            if(bought || manageable){
                if(postMapper.insert(post)>0){

                    return new ResultVO<PostVO>(Constant.REQUEST_SUCCESS, "帖子创建成功。", new PostVO(post,userName,courseName));
                }
                return new ResultVO<>(Constant.REQUEST_FAIL,"创建失败");
            }
            return new ResultVO<>(Constant.REQUEST_FAIL,"用户无权限");
        }
        return new ResultVO<>(Constant.REQUEST_FAIL,"用户名不存在");


    }

    @Override
    public PageInfo<PostVO> getPostByPostTime(Integer currPage, Integer pageSize, Integer uid, Integer courseId) {
        if (currPage == null || currPage < 1) currPage = 1;
        PageHelper.startPage(currPage, pageSize);
        PageInfo<Post> po = new PageInfo<>(postMapper.selectByPostTime(courseId));
        return PageInfoUtil.convert(po, PostVO.class);
    }

    @Override
    public PageInfo<PostVO> getPostByLastReplyTime(Integer currPage, Integer pageSize, Integer uid, Integer courseId) {
        if (currPage == null || currPage < 1) currPage = 1;
        PageHelper.startPage(currPage, pageSize);
        PageInfo<Post> po = new PageInfo<>(postMapper.selectBylastReplyTime(courseId));
        return PageInfoUtil.convert(po, PostVO.class);
    }


    @Override
    public ResultVO<PostVO> getPost(Integer postId,Integer uid) {
        Post post = postMapper.selectByPrimaryKey(postId);
        Integer courseId = post.getCourseId();
        String userName = userService.getUser(uid).getUname();
        String courseName = courseService.getCourse(courseId,uid).getName();
        if(uid != null && uid > 0) {
            boolean bought=courseService.getCourse(courseId,uid).isBought();
            boolean manageable = courseService.getCourse(courseId,uid).isManageable();
            if(bought || manageable){
                return new ResultVO<>(Constant.REQUEST_SUCCESS,"查询成功",new PostVO(post,userName,courseName));
            }
            return new ResultVO<>(Constant.REQUEST_FAIL,"用户无权限");
        }
        return new ResultVO<>(Constant.REQUEST_FAIL,"用户不存在");
    }

    @Override
    public Integer findUserIdByPostId(Integer postId) {
        return postMapper.selectByPrimaryKey(postId).getUserId();
    }

    @Override
    public boolean getPriority(Integer uid,Integer courseId) {
        boolean bought = false;
        boolean manageable = false;
        if(uid==null && uid>0){
            CourseOrder order = orderService.queryMostRecentOrder(uid, courseId);
            if(order != null)
                bought = order.getStatus().equals(Constant.ORDER_STATUS_SUCCESS);
            manageable = uid.equals(courseService.getByPrimaryKey(courseId).getTeacherId());
            if(bought || manageable) return true;
            return false;
        }
        return false;
    }
}
